// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// package main -- go2cs converted at 2022 March 13 05:58:47 UTC
// Original source: C:\Program Files\Go\src\cmd\cgo\out.go
namespace go;

using bytes = bytes_package;
using pkgpath = cmd.@internal.pkgpath_package;
using elf = debug.elf_package;
using macho = debug.macho_package;
using pe = debug.pe_package;
using fmt = fmt_package;
using ast = go.ast_package;
using printer = go.printer_package;
using token = go.token_package;
using exec = @internal.execabs_package;
using xcoff = @internal.xcoff_package;
using io = io_package;
using os = os_package;
using filepath = path.filepath_package;
using regexp = regexp_package;
using sort = sort_package;
using strings = strings_package;
using unicode = unicode_package;
using System;

public static partial class main_package {

private static printer.Config conf = new printer.Config(Mode:printer.SourcePos,Tabwidth:8);private static printer.Config noSourceConf = new printer.Config(Tabwidth:8);

// writeDefs creates output files to be compiled by gc and gcc.
private static void writeDefs(this ptr<Package> _addr_p) => func((defer, panic, _) => {
    ref Package p = ref _addr_p.val;

    io.Writer fgo2 = default;    io.Writer fc = default;

    var f = creat(objDir + "_cgo_gotypes.go".val);
    defer(f.Close());
    fgo2 = f;
    if (gccgo.val) {
        f = creat(objDir + "_cgo_defun.c".val);
        defer(f.Close());
        fc = f;
    }
    var fm = creat(objDir + "_cgo_main.c".val);

    ref bytes.Buffer gccgoInit = ref heap(out ptr<bytes.Buffer> _addr_gccgoInit);

    var fflg = creat(objDir + "_cgo_flags".val);
    foreach (var (k, v) in p.CgoFlags) {
        fmt.Fprintf(fflg, "_CGO_%s=%s\n", k, strings.Join(v, " "));
        if (k == "LDFLAGS" && !gccgo.val) {
            foreach (var (_, arg) in v) {
                fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg);
            }
        }
    }    fflg.Close(); 

    // Write C main file for using gcc to resolve imports.
    fmt.Fprintf(fm, "int main() { return 0; }\n");
    if (importRuntimeCgo.val) {
        fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt) { }\n");
        fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void) { return 0; }\n");
        fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt) { }\n");
        fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n");
    }
    else
 { 
        // If we're not importing runtime/cgo, we *are* runtime/cgo,
        // which provides these functions. We just need a prototype.
        fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt);\n");
        fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n");
        fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__);\n");
    }
    fmt.Fprintf(fm, "void _cgo_allocate(void *a, int c) { }\n");
    fmt.Fprintf(fm, "void _cgo_panic(void *a, int c) { }\n");
    fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n"); 

    // Write second Go output: definitions of _C_xxx.
    // In a separate file so that the import of "unsafe" does not
    // pollute the original file.
    fmt.Fprintf(fgo2, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n");
    fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName);
    fmt.Fprintf(fgo2, "import \"unsafe\"\n\n");
    if (!gccgo && importRuntimeCgo.val) {
        fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n");
    }
    if (importSyscall.val) {
        fmt.Fprintf(fgo2, "import \"syscall\"\n\n");
        fmt.Fprintf(fgo2, "var _ syscall.Errno\n");
    }
    fmt.Fprintf(fgo2, "func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }\n\n");

    if (!gccgo.val) {
        fmt.Fprintf(fgo2, "//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse\n");
        fmt.Fprintf(fgo2, "var _Cgo_always_false bool\n");
        fmt.Fprintf(fgo2, "//go:linkname _Cgo_use runtime.cgoUse\n");
        fmt.Fprintf(fgo2, "func _Cgo_use(interface{})\n");
    }
    var typedefNames = make_slice<@string>(0, len(typedef));
    {
        var name__prev1 = name;

        foreach (var (__name) in typedef) {
            name = __name;
            if (name == "_Ctype_void") { 
                // We provide an appropriate declaration for
                // _Ctype_void below (#39877).
                continue;
            }
            typedefNames = append(typedefNames, name);
        }
        name = name__prev1;
    }

    sort.Strings(typedefNames);
    {
        var name__prev1 = name;

        foreach (var (_, __name) in typedefNames) {
            name = __name;
            var def = typedef[name];
            if (def.NotInHeap) {
                fmt.Fprintf(fgo2, "//go:notinheap\n");
            }
            fmt.Fprintf(fgo2, "type %s ", name); 
            // We don't have source info for these types, so write them out without source info.
            // Otherwise types would look like:
            //
            // type _Ctype_struct_cb struct {
            // //line :1
            //        on_test *[0]byte
            // //line :1
            // }
            //
            // Which is not useful. Moreover we never override source info,
            // so subsequent source code uses the same source info.
            // Moreover, empty file name makes compile emit no source debug info at all.
            ref bytes.Buffer buf = ref heap(out ptr<bytes.Buffer> _addr_buf);
            noSourceConf.Fprint(_addr_buf, fset, def.Go);
            if (bytes.HasPrefix(buf.Bytes(), (slice<byte>)"_Ctype_") || strings.HasPrefix(name, "_Ctype_enum_") || strings.HasPrefix(name, "_Ctype_union_")) { 
                // This typedef is of the form `typedef a b` and should be an alias.
                fmt.Fprintf(fgo2, "= ");
            }
            fmt.Fprintf(fgo2, "%s", buf.Bytes());
            fmt.Fprintf(fgo2, "\n\n");
        }
        name = name__prev1;
    }

    if (gccgo.val) {
        fmt.Fprintf(fgo2, "type _Ctype_void byte\n");
    }
    else
 {
        fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n");
    }
    if (gccgo.val) {
        fmt.Fprint(fgo2, gccgoGoProlog);
        fmt.Fprint(fc, p.cPrologGccgo());
    }
    else
 {
        fmt.Fprint(fgo2, goProlog);
    }
    if (fc != null) {
        fmt.Fprintf(fc, "#line 1 \"cgo-generated-wrappers\"\n");
    }
    if (fm != null) {
        fmt.Fprintf(fm, "#line 1 \"cgo-generated-wrappers\"\n");
    }
    var gccgoSymbolPrefix = p.gccgoSymbolPrefix();

    var cVars = make_map<@string, bool>();
    {
        var key__prev1 = key;

        foreach (var (_, __key) in nameKeys(p.Name)) {
            key = __key;
            var n = p.Name[key];
            if (!n.IsVar()) {
                continue;
            }
            if (!cVars[n.C]) {
                if (gccgo.val) {
                    fmt.Fprintf(fc, "extern byte *%s;\n", n.C);
                }
                else
 { 
                    // Force a reference to all symbols so that
                    // the external linker will add DT_NEEDED
                    // entries as needed on ELF systems.
                    // Treat function variables differently
                    // to avoid type confict errors from LTO
                    // (Link Time Optimization).
                    if (n.Kind == "fpvar") {
                        fmt.Fprintf(fm, "extern void %s();\n", n.C);
                    }
                    else
 {
                        fmt.Fprintf(fm, "extern char %s[];\n", n.C);
                        fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C);
                    }
                    fmt.Fprintf(fgo2, "//go:linkname __cgo_%s %s\n", n.C, n.C);
                    fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", n.C);
                    fmt.Fprintf(fgo2, "var __cgo_%s byte\n", n.C);
                }
                cVars[n.C] = true;
            }
            ast.Node node = default;
            if (n.Kind == "var") {
                node = addr(new ast.StarExpr(X:n.Type.Go));
            }
            else if (n.Kind == "fpvar") {
                node = n.Type.Go;
            }
            else
 {
                panic(fmt.Errorf("invalid var kind %q", n.Kind));
            }
            if (gccgo.val) {
                fmt.Fprintf(fc, "extern void *%s __asm__(\"%s.%s\");", n.Mangle, gccgoSymbolPrefix, gccgoToSymbol(n.Mangle));
                fmt.Fprintf(_addr_gccgoInit, "\t%s = &%s;\n", n.Mangle, n.C);
                fmt.Fprintf(fc, "\n");
            }
            fmt.Fprintf(fgo2, "var %s ", n.Mangle);
            conf.Fprint(fgo2, fset, node);
            if (!gccgo.val) {
                fmt.Fprintf(fgo2, " = (");
                conf.Fprint(fgo2, fset, node);
                fmt.Fprintf(fgo2, ")(unsafe.Pointer(&__cgo_%s))", n.C);
            }
            fmt.Fprintf(fgo2, "\n");
        }
        key = key__prev1;
    }

    if (gccgo.val) {
        fmt.Fprintf(fc, "\n");
    }
    {
        var key__prev1 = key;

        foreach (var (_, __key) in nameKeys(p.Name)) {
            key = __key;
            n = p.Name[key];
            if (n.Const != "") {
                fmt.Fprintf(fgo2, "const %s = %s\n", n.Mangle, n.Const);
            }
        }
        key = key__prev1;
    }

    fmt.Fprintf(fgo2, "\n");

    ref var callsMalloc = ref heap(false, out ptr<var> _addr_callsMalloc);
    {
        var key__prev1 = key;

        foreach (var (_, __key) in nameKeys(p.Name)) {
            key = __key;
            n = p.Name[key];
            if (n.FuncType != null) {
                p.writeDefsFunc(fgo2, n, _addr_callsMalloc);
            }
        }
        key = key__prev1;
    }

    var fgcc = creat(objDir + "_cgo_export.c".val);
    var fgcch = creat(objDir + "_cgo_export.h".val);
    if (gccgo.val) {
        p.writeGccgoExports(fgo2, fm, fgcc, fgcch);
    }
    else
 {
        p.writeExports(fgo2, fm, fgcc, fgcch);
    }
    if (callsMalloc && !gccgo.val) {
        fmt.Fprint(fgo2, strings.Replace(cMallocDefGo, "PREFIX", cPrefix, -1));
        fmt.Fprint(fgcc, strings.Replace(strings.Replace(cMallocDefC, "PREFIX", cPrefix, -1), "PACKED", p.packedAttribute(), -1));
    }
    {
        var err__prev1 = err;

        var err = fgcc.Close();

        if (err != null) {
            fatalf("%s", err);
        }
        err = err__prev1;

    }
    {
        var err__prev1 = err;

        err = fgcch.Close();

        if (err != null) {
            fatalf("%s", err);
        }
        err = err__prev1;

    }

    if (exportHeader != "" && len(p.ExpFunc) > 0.val) {
        var fexp = creat(exportHeader.val);
        var (fgcch, err) = os.Open(objDir + "_cgo_export.h".val);
        if (err != null) {
            fatalf("%s", err);
        }
        defer(fgcch.Close());
        _, err = io.Copy(fexp, fgcch);
        if (err != null) {
            fatalf("%s", err);
        }
        err = fexp.Close();

        if (err != null) {
            fatalf("%s", err);
        }
    }
    var init = gccgoInit.String();
    if (init != "") { 
        // The init function does nothing but simple
        // assignments, so it won't use much stack space, so
        // it's OK to not split the stack. Splitting the stack
        // can run into a bug in clang (as of 2018-11-09):
        // this is a leaf function, and when clang sees a leaf
        // function it won't emit the split stack prologue for
        // the function. However, if this function refers to a
        // non-split-stack function, which will happen if the
        // cgo code refers to a C function not compiled with
        // -fsplit-stack, then the linker will think that it
        // needs to adjust the split stack prologue, but there
        // won't be one. Marking the function explicitly
        // no_split_stack works around this problem by telling
        // the linker that it's OK if there is no split stack
        // prologue.
        fmt.Fprintln(fc, "static void init(void) __attribute__ ((constructor, no_split_stack));");
        fmt.Fprintln(fc, "static void init(void) {");
        fmt.Fprint(fc, init);
        fmt.Fprintln(fc, "}");
    }
});

// elfImportedSymbols is like elf.File.ImportedSymbols, but it
// includes weak symbols.
//
// A bug in some versions of LLD (at least LLD 8) cause it to emit
// several pthreads symbols as weak, but we need to import those. See
// issue #31912 or https://bugs.llvm.org/show_bug.cgi?id=42442.
//
// When doing external linking, we hand everything off to the external
// linker, which will create its own dynamic symbol tables. For
// internal linking, this may turn weak imports into strong imports,
// which could cause dynamic linking to fail if a symbol really isn't
// defined. However, the standard library depends on everything it
// imports, and this is the primary use of dynamic symbol tables with
// internal linking.
private static slice<elf.ImportedSymbol> elfImportedSymbols(ptr<elf.File> _addr_f) {
    ref elf.File f = ref _addr_f.val;

    var (syms, _) = f.DynamicSymbols();
    slice<elf.ImportedSymbol> imports = default;
    foreach (var (_, s) in syms) {
        if ((elf.ST_BIND(s.Info) == elf.STB_GLOBAL || elf.ST_BIND(s.Info) == elf.STB_WEAK) && s.Section == elf.SHN_UNDEF) {
            imports = append(imports, new elf.ImportedSymbol(Name:s.Name,Library:s.Library,Version:s.Version,));
        }
    }    return imports;
}

private static void dynimport(@string obj) {
    var stdout = os.Stdout;
    if (dynout != "".val) {
        var (f, err) = os.Create(dynout.val);
        if (err != null) {
            fatalf("%s", err);
        }
        stdout = f;
    }
    fmt.Fprintf(stdout, "package %s\n", dynpackage.val);

    {
        var f__prev1 = f;

        (f, err) = elf.Open(obj);

        if (err == null) {
            if (dynlinker.val) { 
                // Emit the cgo_dynamic_linker line.
                {
                    var sec = f.Section(".interp");

                    if (sec != null) {
                        {
                            var (data, err) = sec.Data();

                            if (err == null && len(data) > 1) { 
                                // skip trailing \0 in data
                                fmt.Fprintf(stdout, "//go:cgo_dynamic_linker %q\n", string(data[..(int)len(data) - 1]));
                            }

                        }
                    }

                }
            }
            var sym = elfImportedSymbols(_addr_f);
            {
                var s__prev1 = s;

                foreach (var (_, __s) in sym) {
                    s = __s;
                    var targ = s.Name;
                    if (s.Version != "") {
                        targ += "#" + s.Version;
                    }
                    checkImportSymName(s.Name);
                    checkImportSymName(targ);
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, targ, s.Library);
                }

                s = s__prev1;
            }

            var (lib, _) = f.ImportedLibraries();
            {
                var l__prev1 = l;

                foreach (var (_, __l) in lib) {
                    l = __l;
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l);
                }

                l = l__prev1;
            }

            return ;
        }
        f = f__prev1;

    }

    {
        var f__prev1 = f;

        (f, err) = macho.Open(obj);

        if (err == null) {
            var (sym, _) = f.ImportedSymbols();
            {
                var s__prev1 = s;

                foreach (var (_, __s) in sym) {
                    s = __s;
                    if (len(s) > 0 && s[0] == '_') {
                        s = s[(int)1..];
                    }
                    checkImportSymName(s);
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "");
                }

                s = s__prev1;
            }

            (lib, _) = f.ImportedLibraries();
            {
                var l__prev1 = l;

                foreach (var (_, __l) in lib) {
                    l = __l;
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l);
                }

                l = l__prev1;
            }

            return ;
        }
        f = f__prev1;

    }

    {
        var f__prev1 = f;

        (f, err) = pe.Open(obj);

        if (err == null) {
            (sym, _) = f.ImportedSymbols();
            {
                var s__prev1 = s;

                foreach (var (_, __s) in sym) {
                    s = __s;
                    var ss = strings.Split(s, ":");
                    var name = strings.Split(ss[0], "@")[0];
                    checkImportSymName(name);
                    checkImportSymName(ss[0]);
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", name, ss[0], strings.ToLower(ss[1]));
                }

                s = s__prev1;
            }

            return ;
        }
        f = f__prev1;

    }

    {
        var f__prev1 = f;

        (f, err) = xcoff.Open(obj);

        if (err == null) {
            var (sym, err) = f.ImportedSymbols();
            if (err != null) {
                fatalf("cannot load imported symbols from XCOFF file %s: %v", obj, err);
            }
            {
                var s__prev1 = s;

                foreach (var (_, __s) in sym) {
                    s = __s;
                    if (s.Name == "runtime_rt0_go" || s.Name == "_rt0_ppc64_aix_lib") { 
                        // These symbols are imported by runtime/cgo but
                        // must not be added to _cgo_import.go as there are
                        // Go symbols.
                        continue;
                    }
                    checkImportSymName(s.Name);
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, s.Name, s.Library);
                }

                s = s__prev1;
            }

            var (lib, err) = f.ImportedLibraries();
            if (err != null) {
                fatalf("cannot load imported libraries from XCOFF file %s: %v", obj, err);
            }
            {
                var l__prev1 = l;

                foreach (var (_, __l) in lib) {
                    l = __l;
                    fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l);
                }

                l = l__prev1;
            }

            return ;
        }
        f = f__prev1;

    }

    fatalf("cannot parse %s as ELF, Mach-O, PE or XCOFF", obj);
}

// checkImportSymName checks a symbol name we are going to emit as part
// of a //go:cgo_import_dynamic pragma. These names come from object
// files, so they may be corrupt. We are going to emit them unquoted,
// so while they don't need to be valid symbol names (and in some cases,
// involving symbol versions, they won't be) they must contain only
// graphic characters and must not contain Go comments.
private static void checkImportSymName(@string s) {
    foreach (var (_, c) in s) {
        if (!unicode.IsGraphic(c) || unicode.IsSpace(c)) {
            fatalf("dynamic symbol %q contains unsupported character", s);
        }
    }    if (strings.Index(s, "//") >= 0 || strings.Index(s, "/*") >= 0) {
        fatalf("dynamic symbol %q contains Go comment");
    }
}

// Construct a gcc struct matching the gc argument frame.
// Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
// These assumptions are checked by the gccProlog.
// Also assumes that gc convention is to word-align the
// input and output parameters.
private static (@string, long) structType(this ptr<Package> _addr_p, ptr<Name> _addr_n) {
    @string _p0 = default;
    long _p0 = default;
    ref Package p = ref _addr_p.val;
    ref Name n = ref _addr_n.val;

    ref bytes.Buffer buf = ref heap(out ptr<bytes.Buffer> _addr_buf);
    fmt.Fprint(_addr_buf, "struct {\n");
    var off = int64(0);
    {
        var t__prev1 = t;

        foreach (var (__i, __t) in n.FuncType.Params) {
            i = __i;
            t = __t;
            if (off % t.Align != 0) {
                var pad = t.Align - off % t.Align;
                fmt.Fprintf(_addr_buf, "\t\tchar __pad%d[%d];\n", off, pad);
                off += pad;
            }
            var c = t.Typedef;
            if (c == "") {
                c = t.C.String();
            }
            fmt.Fprintf(_addr_buf, "\t\t%s p%d;\n", c, i);
            off += t.Size;
        }
        t = t__prev1;
    }

    if (off % p.PtrSize != 0) {
        pad = p.PtrSize - off % p.PtrSize;
        fmt.Fprintf(_addr_buf, "\t\tchar __pad%d[%d];\n", off, pad);
        off += pad;
    }
    {
        var t__prev1 = t;

        var t = n.FuncType.Result;

        if (t != null) {
            if (off % t.Align != 0) {
                pad = t.Align - off % t.Align;
                fmt.Fprintf(_addr_buf, "\t\tchar __pad%d[%d];\n", off, pad);
                off += pad;
            }
            fmt.Fprintf(_addr_buf, "\t\t%s r;\n", t.C);
            off += t.Size;
        }
        t = t__prev1;

    }
    if (off % p.PtrSize != 0) {
        pad = p.PtrSize - off % p.PtrSize;
        fmt.Fprintf(_addr_buf, "\t\tchar __pad%d[%d];\n", off, pad);
        off += pad;
    }
    if (off == 0) {
        fmt.Fprintf(_addr_buf, "\t\tchar unused;\n"); // avoid empty struct
    }
    fmt.Fprintf(_addr_buf, "\t}");
    return (buf.String(), off);
}

private static void writeDefsFunc(this ptr<Package> _addr_p, io.Writer fgo2, ptr<Name> _addr_n, ptr<bool> _addr_callsMalloc) {
    ref Package p = ref _addr_p.val;
    ref Name n = ref _addr_n.val;
    ref bool callsMalloc = ref _addr_callsMalloc.val;

    var name = n.Go;
    var gtype = n.FuncType.Go;
    var @void = gtype.Results == null || len(gtype.Results.List) == 0;
    if (n.AddError) { 
        // Add "error" to return type list.
        // Type list is known to be 0 or 1 element - it's a C function.
        ptr<ast.Field> err = addr(new ast.Field(Type:ast.NewIdent("error")));
        var l = gtype.Results.List;
        if (len(l) == 0) {
            l = new slice<ptr<ast.Field>>(new ptr<ast.Field>[] { err });
        }
        else
 {
            l = new slice<ptr<ast.Field>>(new ptr<ast.Field>[] { l[0], err });
        }
        ptr<object> t = @new<ast.FuncType>();
        t.val = gtype.val;
        t.Results = addr(new ast.FieldList(List:l));
        gtype = t;
    }
    ptr<ast.FuncDecl> d = addr(new ast.FuncDecl(Name:ast.NewIdent(n.Mangle),Type:gtype,)); 

    // Builtins defined in the C prolog.
    var inProlog = builtinDefs[name] != "";
    var cname = fmt.Sprintf("_cgo%s%s", cPrefix, n.Mangle);
    slice<@string> paramnames = (slice<@string>)null;
    if (d.Type.Params != null) {
        {
            var i__prev1 = i;

            foreach (var (__i, __param) in d.Type.Params.List) {
                i = __i;
                param = __param;
                var paramName = fmt.Sprintf("p%d", i);
                param.Names = new slice<ptr<ast.Ident>>(new ptr<ast.Ident>[] { ast.NewIdent(paramName) });
                paramnames = append(paramnames, paramName);
            }

            i = i__prev1;
        }
    }
    if (gccgo.val) { 
        // Gccgo style hooks.
        fmt.Fprint(fgo2, "\n");
        conf.Fprint(fgo2, fset, d);
        fmt.Fprint(fgo2, " {\n");
        if (!inProlog) {
            fmt.Fprint(fgo2, "\tdefer syscall.CgocallDone()\n");
            fmt.Fprint(fgo2, "\tsyscall.Cgocall()\n");
        }
        if (n.AddError) {
            fmt.Fprint(fgo2, "\tsyscall.SetErrno(0)\n");
        }
        fmt.Fprint(fgo2, "\t");
        if (!void) {
            fmt.Fprint(fgo2, "r := ");
        }
        fmt.Fprintf(fgo2, "%s(%s)\n", cname, strings.Join(paramnames, ", "));

        if (n.AddError) {
            fmt.Fprint(fgo2, "\te := syscall.GetErrno()\n");
            fmt.Fprint(fgo2, "\tif e != 0 {\n");
            fmt.Fprint(fgo2, "\t\treturn ");
            if (!void) {
                fmt.Fprint(fgo2, "r, ");
            }
            fmt.Fprint(fgo2, "e\n");
            fmt.Fprint(fgo2, "\t}\n");
            fmt.Fprint(fgo2, "\treturn ");
            if (!void) {
                fmt.Fprint(fgo2, "r, ");
            }
            fmt.Fprint(fgo2, "nil\n");
        }
        else if (!void) {
            fmt.Fprint(fgo2, "\treturn r\n");
        }
        fmt.Fprint(fgo2, "}\n"); 

        // declare the C function.
        fmt.Fprintf(fgo2, "//extern %s\n", cname);
        d.Name = ast.NewIdent(cname);
        if (n.AddError) {
            l = d.Type.Results.List;
            d.Type.Results.List = l[..(int)len(l) - 1];
        }
        conf.Fprint(fgo2, fset, d);
        fmt.Fprint(fgo2, "\n");

        return ;
    }
    if (inProlog) {
        fmt.Fprint(fgo2, builtinDefs[name]);
        if (strings.Contains(builtinDefs[name], "_cgo_cmalloc")) {
            callsMalloc = true;
        }
        return ;
    }
    fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", cname);
    fmt.Fprintf(fgo2, "//go:linkname __cgofn_%s %s\n", cname, cname);
    fmt.Fprintf(fgo2, "var __cgofn_%s byte\n", cname);
    fmt.Fprintf(fgo2, "var %s = unsafe.Pointer(&__cgofn_%s)\n", cname, cname);

    nint nret = 0;
    if (!void) {
        d.Type.Results.List[0].Names = new slice<ptr<ast.Ident>>(new ptr<ast.Ident>[] { ast.NewIdent("r1") });
        nret = 1;
    }
    if (n.AddError) {
        d.Type.Results.List[nret].Names = new slice<ptr<ast.Ident>>(new ptr<ast.Ident>[] { ast.NewIdent("r2") });
    }
    fmt.Fprint(fgo2, "\n");
    fmt.Fprint(fgo2, "//go:cgo_unsafe_args\n");
    conf.Fprint(fgo2, fset, d);
    fmt.Fprint(fgo2, " {\n"); 

    // NOTE: Using uintptr to hide from escape analysis.
    @string arg = "0";
    if (len(paramnames) > 0) {
        arg = "uintptr(unsafe.Pointer(&p0))";
    }
    else if (!void) {
        arg = "uintptr(unsafe.Pointer(&r1))";
    }
    @string prefix = "";
    if (n.AddError) {
        prefix = "errno := ";
    }
    fmt.Fprintf(fgo2, "\t%s_cgo_runtime_cgocall(%s, %s)\n", prefix, cname, arg);
    if (n.AddError) {
        fmt.Fprintf(fgo2, "\tif errno != 0 { r2 = syscall.Errno(errno) }\n");
    }
    fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n");
    if (d.Type.Params != null) {
        {
            var i__prev1 = i;

            foreach (var (__i) in d.Type.Params.List) {
                i = __i;
                fmt.Fprintf(fgo2, "\t\t_Cgo_use(p%d)\n", i);
            }

            i = i__prev1;
        }
    }
    fmt.Fprintf(fgo2, "\t}\n");
    fmt.Fprintf(fgo2, "\treturn\n");
    fmt.Fprintf(fgo2, "}\n");
}

// writeOutput creates stubs for a specific source file to be compiled by gc
private static void writeOutput(this ptr<Package> _addr_p, ptr<File> _addr_f, @string srcfile) {
    ref Package p = ref _addr_p.val;
    ref File f = ref _addr_f.val;

    var @base = srcfile;
    if (strings.HasSuffix(base, ".go")) {
        base = base[(int)0..(int)len(base) - 3];
    }
    base = filepath.Base(base);
    var fgo1 = creat(objDir + base + ".cgo1.go".val);
    var fgcc = creat(objDir + base + ".cgo2.c".val);

    p.GoFiles = append(p.GoFiles, base + ".cgo1.go");
    p.GccFiles = append(p.GccFiles, base + ".cgo2.c"); 

    // Write Go output: Go input with rewrites of C.xxx to _C_xxx.
    fmt.Fprintf(fgo1, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n");
    fmt.Fprintf(fgo1, "//line %s:1:1\n", srcfile);
    fgo1.Write(f.Edit.Bytes()); 

    // While we process the vars and funcs, also write gcc output.
    // Gcc output starts with the preamble.
    fmt.Fprintf(fgcc, "%s\n", builtinProlog);
    fmt.Fprintf(fgcc, "%s\n", f.Preamble);
    fmt.Fprintf(fgcc, "%s\n", gccProlog);
    fmt.Fprintf(fgcc, "%s\n", tsanProlog);
    fmt.Fprintf(fgcc, "%s\n", msanProlog);

    foreach (var (_, key) in nameKeys(f.Name)) {
        var n = f.Name[key];
        if (n.FuncType != null) {
            p.writeOutputFunc(fgcc, n);
        }
    }    fgo1.Close();
    fgcc.Close();
}

// fixGo converts the internal Name.Go field into the name we should show
// to users in error messages. There's only one for now: on input we rewrite
// C.malloc into C._CMalloc, so change it back here.
private static @string fixGo(@string name) {
    if (name == "_CMalloc") {
        return "malloc";
    }
    return name;
}

private static map isBuiltin = /* TODO: Fix this in ScannerBase_Expression::ExitCompositeLit */ new map<@string, bool>{"_Cfunc_CString":true,"_Cfunc_CBytes":true,"_Cfunc_GoString":true,"_Cfunc_GoStringN":true,"_Cfunc_GoBytes":true,"_Cfunc__CMalloc":true,};

private static void writeOutputFunc(this ptr<Package> _addr_p, ptr<os.File> _addr_fgcc, ptr<Name> _addr_n) {
    ref Package p = ref _addr_p.val;
    ref os.File fgcc = ref _addr_fgcc.val;
    ref Name n = ref _addr_n.val;

    var name = n.Mangle;
    if (isBuiltin[name] || p.Written[name]) { 
        // The builtins are already defined in the C prolog, and we don't
        // want to duplicate function definitions we've already done.
        return ;
    }
    p.Written[name] = true;

    if (gccgo.val) {
        p.writeGccgoOutputFunc(fgcc, n);
        return ;
    }
    var (ctype, _) = p.structType(n); 

    // Gcc wrapper unpacks the C argument struct
    // and calls the actual C function.
    fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n");
    if (n.AddError) {
        fmt.Fprintf(fgcc, "int\n");
    }
    else
 {
        fmt.Fprintf(fgcc, "void\n");
    }
    fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle);
    fmt.Fprintf(fgcc, "{\n");
    if (n.AddError) {
        fmt.Fprintf(fgcc, "\tint _cgo_errno;\n");
    }
    fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute());
    if (n.FuncType.Result != null) { 
        // Save the stack top for use below.
        fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n");
    }
    var tr = n.FuncType.Result;
    if (tr != null) {
        fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n");
    }
    fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n");
    if (n.AddError) {
        fmt.Fprintf(fgcc, "\terrno = 0;\n");
    }
    fmt.Fprintf(fgcc, "\t");
    if (tr != null) {
        fmt.Fprintf(fgcc, "_cgo_r = ");
        {
            var c = tr.C.String();

            if (c[len(c) - 1] == '*') {
                fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ");
            }

        }
    }
    if (n.Kind == "macro") {
        fmt.Fprintf(fgcc, "%s;\n", n.C);
    }
    else
 {
        fmt.Fprintf(fgcc, "%s(", n.C);
        foreach (var (i) in n.FuncType.Params) {
            if (i > 0) {
                fmt.Fprintf(fgcc, ", ");
            }
            fmt.Fprintf(fgcc, "_cgo_a->p%d", i);
        }        fmt.Fprintf(fgcc, ");\n");
    }
    if (n.AddError) {
        fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n");
    }
    fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n");
    if (n.FuncType.Result != null) { 
        // The cgo call may have caused a stack copy (via a callback).
        // Adjust the return value pointer appropriately.
        fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n"); 
        // Save the return value.
        fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n"); 
        // The return value is on the Go stack. If we are using msan,
        // and if the C value is partially or completely uninitialized,
        // the assignment will mark the Go stack as uninitialized.
        // The Go compiler does not update msan for changes to the
        // stack. It is possible that the stack will remain
        // uninitialized, and then later be used in a way that is
        // visible to msan, possibly leading to a false positive.
        // Mark the stack space as written, to avoid this problem.
        // See issue 26209.
        fmt.Fprintf(fgcc, "\t_cgo_msan_write(&_cgo_a->r, sizeof(_cgo_a->r));\n");
    }
    if (n.AddError) {
        fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n");
    }
    fmt.Fprintf(fgcc, "}\n");
    fmt.Fprintf(fgcc, "\n");
}

// Write out a wrapper for a function when using gccgo. This is a
// simple wrapper that just calls the real function. We only need a
// wrapper to support static functions in the prologue--without a
// wrapper, we can't refer to the function, since the reference is in
// a different file.
private static void writeGccgoOutputFunc(this ptr<Package> _addr_p, ptr<os.File> _addr_fgcc, ptr<Name> _addr_n) {
    ref Package p = ref _addr_p.val;
    ref os.File fgcc = ref _addr_fgcc.val;
    ref Name n = ref _addr_n.val;

    fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n");
    {
        var t__prev1 = t;

        var t = n.FuncType.Result;

        if (t != null) {
            fmt.Fprintf(fgcc, "%s\n", t.C.String());
        }
        else
 {
            fmt.Fprintf(fgcc, "void\n");
        }
        t = t__prev1;

    }
    fmt.Fprintf(fgcc, "_cgo%s%s(", cPrefix, n.Mangle);
    {
        var i__prev1 = i;
        var t__prev1 = t;

        foreach (var (__i, __t) in n.FuncType.Params) {
            i = __i;
            t = __t;
            if (i > 0) {
                fmt.Fprintf(fgcc, ", ");
            }
            var c = t.Typedef;
            if (c == "") {
                c = t.C.String();
            }
            fmt.Fprintf(fgcc, "%s p%d", c, i);
        }
        i = i__prev1;
        t = t__prev1;
    }

    fmt.Fprintf(fgcc, ")\n");
    fmt.Fprintf(fgcc, "{\n");
    {
        var t__prev1 = t;

        t = n.FuncType.Result;

        if (t != null) {
            fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String());
        }
        t = t__prev1;

    }
    fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n");
    fmt.Fprintf(fgcc, "\t");
    {
        var t__prev1 = t;

        t = n.FuncType.Result;

        if (t != null) {
            fmt.Fprintf(fgcc, "_cgo_r = "); 
            // Cast to void* to avoid warnings due to omitted qualifiers.
            {
                var c__prev2 = c;

                c = t.C.String();

                if (c[len(c) - 1] == '*') {
                    fmt.Fprintf(fgcc, "(void*)");
                }

                c = c__prev2;

            }
        }
        t = t__prev1;

    }
    if (n.Kind == "macro") {
        fmt.Fprintf(fgcc, "%s;\n", n.C);
    }
    else
 {
        fmt.Fprintf(fgcc, "%s(", n.C);
        {
            var i__prev1 = i;

            foreach (var (__i) in n.FuncType.Params) {
                i = __i;
                if (i > 0) {
                    fmt.Fprintf(fgcc, ", ");
                }
                fmt.Fprintf(fgcc, "p%d", i);
            }

            i = i__prev1;
        }

        fmt.Fprintf(fgcc, ");\n");
    }
    fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n");
    {
        var t__prev1 = t;

        t = n.FuncType.Result;

        if (t != null) {
            fmt.Fprintf(fgcc, "\treturn "); 
            // Cast to void* to avoid warnings due to omitted qualifiers
            // and explicit incompatible struct types.
            {
                var c__prev2 = c;

                c = t.C.String();

                if (c[len(c) - 1] == '*') {
                    fmt.Fprintf(fgcc, "(void*)");
                }

                c = c__prev2;

            }
            fmt.Fprintf(fgcc, "_cgo_r;\n");
        }
        t = t__prev1;

    }
    fmt.Fprintf(fgcc, "}\n");
    fmt.Fprintf(fgcc, "\n");
}

// packedAttribute returns host compiler struct attribute that will be
// used to match gc's struct layout. For example, on 386 Windows,
// gcc wants to 8-align int64s, but gc does not.
// Use __gcc_struct__ to work around https://gcc.gnu.org/PR52991 on x86,
// and https://golang.org/issue/5603.
private static @string packedAttribute(this ptr<Package> _addr_p) {
    ref Package p = ref _addr_p.val;

    @string s = "__attribute__((__packed__";
    if (!p.GccIsClang && (goarch == "amd64" || goarch == "386")) {
        s += ", __gcc_struct__";
    }
    return s + "))";
}

// exportParamName returns the value of param as it should be
// displayed in a c header file. If param contains any non-ASCII
// characters, this function will return the character p followed by
// the value of position; otherwise, this function will return the
// value of param.
private static @string exportParamName(@string param, nint position) {
    if (param == "") {
        return fmt.Sprintf("p%d", position);
    }
    var pname = param;

    for (nint i = 0; i < len(param); i++) {
        if (param[i] > unicode.MaxASCII) {
            pname = fmt.Sprintf("p%d", position);
            break;
        }
    }

    return pname;
}

// Write out the various stubs we need to support functions exported
// from Go so that they are callable from C.
private static void writeExports(this ptr<Package> _addr_p, io.Writer fgo2, io.Writer fm, io.Writer fgcc, io.Writer fgcch) {
    ref Package p = ref _addr_p.val;

    p.writeExportHeader(fgcch);

    fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n");
    fmt.Fprintf(fgcc, "#include <stdlib.h>\n");
    fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n"); 

    // We use packed structs, but they are always aligned.
    // The pragmas and address-of-packed-member are only recognized as
    // warning groups in clang 4.0+, so ignore unknown pragmas first.
    fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n");
    fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wpragmas\"\n");
    fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Waddress-of-packed-member\"\n");

    fmt.Fprintf(fgcc, "extern void crosscall2(void (*fn)(void *), void *, int, __SIZE_TYPE__);\n");
    fmt.Fprintf(fgcc, "extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n");
    fmt.Fprintf(fgcc, "extern void _cgo_release_context(__SIZE_TYPE__);\n\n");
    fmt.Fprintf(fgcc, "extern char* _cgo_topofstack(void);");
    fmt.Fprintf(fgcc, "%s\n", tsanProlog);
    fmt.Fprintf(fgcc, "%s\n", msanProlog);

    foreach (var (_, exp) in p.ExpFunc) {
        var fn = exp.Func; 

        // Construct a struct that will be used to communicate
        // arguments from C to Go. The C and Go definitions
        // just have to agree. The gcc struct will be compiled
        // with __attribute__((packed)) so all padding must be
        // accounted for explicitly.
        @string ctype = "struct {\n";
        ptr<bytes.Buffer> gotype = @new<bytes.Buffer>();
        fmt.Fprintf(gotype, "struct {\n");
        var off = int64(0);
        nint npad = 0;
        Action<ast.Expr, @string, object[]> argField = (typ, namePat, args) => {
            var name = fmt.Sprintf(namePat, args);
            var t = p.cgoType(typ);
            if (off % t.Align != 0) {
                var pad = t.Align - off % t.Align;
                ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad);
                off += pad;
                npad++;
            }
            ctype += fmt.Sprintf("\t\t%s %s;\n", t.C, name);
            fmt.Fprintf(gotype, "\t\t%s ", name);
            noSourceConf.Fprint(gotype, fset, typ);
            fmt.Fprintf(gotype, "\n");
            off += t.Size;
        };
        if (fn.Recv != null) {
            argField(fn.Recv.List[0].Type, "recv");
        }
        var fntype = fn.Type;
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            argField(atype, "p%d", i);
        });
        forFieldList(_addr_fntype.Results, (i, aname, atype) => {
            argField(atype, "r%d", i);
        });
        if (ctype == "struct {\n") {
            ctype += "\t\tchar unused;\n"; // avoid empty struct
        }
        ctype += "\t}";
        fmt.Fprintf(gotype, "\t}"); 

        // Get the return type of the wrapper function
        // compiled by gcc.
        @string gccResult = "";
        if (fntype.Results == null || len(fntype.Results.List) == 0) {
            gccResult = "void";
        }
        else if (len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1) {
            gccResult = p.cgoType(fntype.Results.List[0].Type).C.String();
        }
        else
 {
            fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName);
            fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName);
            forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i);
                if (len(aname) > 0) {
                    fmt.Fprintf(fgcch, " /* %s */", aname);
                }
                fmt.Fprint(fgcch, "\n");
            });
            fmt.Fprintf(fgcch, "};\n");
            gccResult = "struct " + exp.ExpName + "_return";
        }
        @string gccExport = "";
        if (goos == "windows") {
            gccExport = "__declspec(dllexport) ";
        }
        var s = fmt.Sprintf("%s%s %s(", gccExport, gccResult, exp.ExpName);
        if (fn.Recv != null) {
            s += p.cgoType(fn.Recv.List[0].Type).C.String();
            s += " recv";
        }
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0 || fn.Recv != null) {
                s += ", ";
            }
            s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i));
        });
        s += ")";

        if (len(exp.Doc) > 0) {
            fmt.Fprintf(fgcch, "\n%s", exp.Doc);
            if (!strings.HasSuffix(exp.Doc, "\n")) {
                fmt.Fprint(fgcch, "\n");
            }
        }
        fmt.Fprintf(fgcch, "extern %s;\n", s);

        fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *);\n", cPrefix, exp.ExpName);
        fmt.Fprintf(fgcc, "\nCGO_NO_SANITIZE_THREAD");
        fmt.Fprintf(fgcc, "\n%s\n", s);
        fmt.Fprintf(fgcc, "{\n");
        fmt.Fprintf(fgcc, "\t__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();\n"); 
        // The results part of the argument structure must be
        // initialized to 0 so the write barriers generated by
        // the assignments to these fields in Go are safe.
        //
        // We use a local static variable to get the zeroed
        // value of the argument type. This avoids including
        // string.h for memset, and is also robust to C++
        // types with constructors. Both GCC and LLVM optimize
        // this into just zeroing _cgo_a.
        fmt.Fprintf(fgcc, "\ttypedef %s %v _cgo_argtype;\n", ctype, p.packedAttribute());
        fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n");
        fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n");
        if (gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1)) {
            fmt.Fprintf(fgcc, "\t%s r;\n", gccResult);
        }
        if (fn.Recv != null) {
            fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n");
        }
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i));
        });
        fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n");
        fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off);
        fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n");
        fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n");
        if (gccResult != "void") {
            if (len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1) {
                fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n");
            }
            else
 {
                forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                    fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i);
                });
                fmt.Fprintf(fgcc, "\treturn r;\n");
            }
        }
        fmt.Fprintf(fgcc, "}\n"); 

        // In internal linking mode, the Go linker sees both
        // the C wrapper written above and the Go wrapper it
        // references. Hence, export the C wrapper (e.g., for
        // if we're building a shared object). The Go linker
        // will resolve the C wrapper's reference to the Go
        // wrapper without a separate export.
        fmt.Fprintf(fgo2, "//go:cgo_export_dynamic %s\n", exp.ExpName); 
        // cgo_export_static refers to a symbol by its linker
        // name, so set the linker name of the Go wrapper.
        fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName); 
        // In external linking mode, the Go linker sees the Go
        // wrapper, but not the C wrapper. For this case,
        // export the Go wrapper so the host linker can
        // resolve the reference from the C wrapper to the Go
        // wrapper.
        fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName); 

        // Build the wrapper function compiled by cmd/compile.
        // This unpacks the argument struct above and calls the Go function.
        fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a *%s) {\n", cPrefix, exp.ExpName, gotype);

        fmt.Fprintf(fm, "void _cgoexp%s_%s(void* p){}\n", cPrefix, exp.ExpName);

        if (gccResult != "void") { 
            // Write results back to frame.
            fmt.Fprintf(fgo2, "\t");
            forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                if (i > 0) {
                    fmt.Fprintf(fgo2, ", ");
                }
                fmt.Fprintf(fgo2, "a.r%d", i);
            });
            fmt.Fprintf(fgo2, " = ");
        }
        if (fn.Recv != null) {
            fmt.Fprintf(fgo2, "a.recv.");
        }
        fmt.Fprintf(fgo2, "%s(", exp.Func.Name);
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0) {
                fmt.Fprint(fgo2, ", ");
            }
            fmt.Fprintf(fgo2, "a.p%d", i);
        });
        fmt.Fprint(fgo2, ")\n");
        if (gccResult != "void") { 
            // Verify that any results don't contain any
            // Go pointers.
            forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                if (!p.hasPointer(null, atype, false)) {
                    return ;
                }
                fmt.Fprintf(fgo2, "\t_cgoCheckResult(a.r%d)\n", i);
            });
        }
        fmt.Fprint(fgo2, "}\n");
    }    fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog);
}

// Write out the C header allowing C code to call exported gccgo functions.
private static void writeGccgoExports(this ptr<Package> _addr_p, io.Writer fgo2, io.Writer fm, io.Writer fgcc, io.Writer fgcch) {
    ref Package p = ref _addr_p.val;

    var gccgoSymbolPrefix = p.gccgoSymbolPrefix();

    p.writeExportHeader(fgcch);

    fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n");
    fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n");

    fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog);
    fmt.Fprintf(fgcc, "%s\n", tsanProlog);
    fmt.Fprintf(fgcc, "%s\n", msanProlog);

    foreach (var (_, exp) in p.ExpFunc) {
        var fn = exp.Func;
        var fntype = fn.Type;

        ptr<bytes.Buffer> cdeclBuf = @new<bytes.Buffer>();
        nint resultCount = 0;
        forFieldList(_addr_fntype.Results, (i, aname, atype) => {
            resultCount++;
        });
        switch (resultCount) {
            case 0: 
                fmt.Fprintf(cdeclBuf, "void");
                break;
            case 1: 
                forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                    var t = p.cgoType(atype);
                    fmt.Fprintf(cdeclBuf, "%s", t.C);
                });
                break;
            default: 
                // Declare a result struct.
                fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName);
                fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName);
                forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                    t = p.cgoType(atype);
                    fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i);
                    if (len(aname) > 0) {
                        fmt.Fprintf(fgcch, " /* %s */", aname);
                    }
                    fmt.Fprint(fgcch, "\n");
                });
                fmt.Fprintf(fgcch, "};\n");
                fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName);
                break;
        }

        var cRet = cdeclBuf.String();

        cdeclBuf = @new<bytes.Buffer>();
        fmt.Fprintf(cdeclBuf, "(");
        if (fn.Recv != null) {
            fmt.Fprintf(cdeclBuf, "%s recv", p.cgoType(fn.Recv.List[0].Type).C.String());
        }
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0 || fn.Recv != null) {
                fmt.Fprintf(cdeclBuf, ", ");
            }
            t = p.cgoType(atype);
            fmt.Fprintf(cdeclBuf, "%s p%d", t.C, i);
        });
        fmt.Fprintf(cdeclBuf, ")");
        var cParams = cdeclBuf.String();

        if (len(exp.Doc) > 0) {
            fmt.Fprintf(fgcch, "\n%s", exp.Doc);
        }
        fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams); 

        // We need to use a name that will be exported by the
        // Go code; otherwise gccgo will make it static and we
        // will not be able to link against it from the C
        // code.
        @string goName = "Cgoexp_" + exp.ExpName;
        fmt.Fprintf(fgcc, "extern %s %s %s __asm__(\"%s.%s\");", cRet, goName, cParams, gccgoSymbolPrefix, gccgoToSymbol(goName));
        fmt.Fprint(fgcc, "\n");

        fmt.Fprint(fgcc, "\nCGO_NO_SANITIZE_THREAD\n");
        fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams);
        if (resultCount > 0) {
            fmt.Fprintf(fgcc, "\t%s r;\n", cRet);
        }
        fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n");
        fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n");
        fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n");
        fmt.Fprint(fgcc, "\t");
        if (resultCount > 0) {
            fmt.Fprint(fgcc, "r = ");
        }
        fmt.Fprintf(fgcc, "%s(", goName);
        if (fn.Recv != null) {
            fmt.Fprint(fgcc, "recv");
        }
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0 || fn.Recv != null) {
                fmt.Fprintf(fgcc, ", ");
            }
            fmt.Fprintf(fgcc, "p%d", i);
        });
        fmt.Fprint(fgcc, ");\n");
        fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n");
        if (resultCount > 0) {
            fmt.Fprint(fgcc, "\treturn r;\n");
        }
        fmt.Fprint(fgcc, "}\n"); 

        // Dummy declaration for _cgo_main.c
        fmt.Fprintf(fm, "char %s[1] __asm__(\"%s.%s\");", goName, gccgoSymbolPrefix, gccgoToSymbol(goName));
        fmt.Fprint(fm, "\n"); 

        // For gccgo we use a wrapper function in Go, in order
        // to call CgocallBack and CgocallBackDone.

        // This code uses printer.Fprint, not conf.Fprint,
        // because we don't want //line comments in the middle
        // of the function types.
        fmt.Fprint(fgo2, "\n");
        fmt.Fprintf(fgo2, "func %s(", goName);
        if (fn.Recv != null) {
            fmt.Fprint(fgo2, "recv ");
            printer.Fprint(fgo2, fset, fn.Recv.List[0].Type);
        }
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0 || fn.Recv != null) {
                fmt.Fprintf(fgo2, ", ");
            }
            fmt.Fprintf(fgo2, "p%d ", i);
            printer.Fprint(fgo2, fset, atype);
        });
        fmt.Fprintf(fgo2, ")");
        if (resultCount > 0) {
            fmt.Fprintf(fgo2, " (");
            forFieldList(_addr_fntype.Results, (i, aname, atype) => {
                if (i > 0) {
                    fmt.Fprint(fgo2, ", ");
                }
                printer.Fprint(fgo2, fset, atype);
            });
            fmt.Fprint(fgo2, ")");
        }
        fmt.Fprint(fgo2, " {\n");
        fmt.Fprint(fgo2, "\tsyscall.CgocallBack()\n");
        fmt.Fprint(fgo2, "\tdefer syscall.CgocallBackDone()\n");
        fmt.Fprint(fgo2, "\t");
        if (resultCount > 0) {
            fmt.Fprint(fgo2, "return ");
        }
        if (fn.Recv != null) {
            fmt.Fprint(fgo2, "recv.");
        }
        fmt.Fprintf(fgo2, "%s(", exp.Func.Name);
        forFieldList(_addr_fntype.Params, (i, aname, atype) => {
            if (i > 0) {
                fmt.Fprint(fgo2, ", ");
            }
            fmt.Fprintf(fgo2, "p%d", i);
        });
        fmt.Fprint(fgo2, ")\n");
        fmt.Fprint(fgo2, "}\n");
    }    fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog);
}

// writeExportHeader writes out the start of the _cgo_export.h file.
private static void writeExportHeader(this ptr<Package> _addr_p, io.Writer fgcch) {
    ref Package p = ref _addr_p.val;

    fmt.Fprintf(fgcch, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n");
    var pkg = importPath.val;
    if (pkg == "") {
        pkg = p.PackagePath;
    }
    fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg);
    fmt.Fprintf(fgcch, "%s\n", builtinExportProlog); 

    // Remove absolute paths from #line comments in the preamble.
    // They aren't useful for people using the header file,
    // and they mean that the header files change based on the
    // exact location of GOPATH.
    var re = regexp.MustCompile("(?m)^(#line\\s+[0-9]+\\s+\")[^\"]*[/\\\\]([^\"]*\")");
    var preamble = re.ReplaceAllString(p.Preamble, "$1$2");

    fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments.  */\n\n");
    fmt.Fprintf(fgcch, "%s\n", preamble);
    fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments.  */\n\n");

    fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog());
}

// gccgoToSymbol converts a name to a mangled symbol for gccgo.
private static @string gccgoToSymbol(@string ppath) {
    if (gccgoMangler == null) {
        error err = default!;
        var cmd = os.Getenv("GCCGO");
        if (cmd == "") {
            cmd, err = exec.LookPath("gccgo");
            if (err != null) {
                fatalf("unable to locate gccgo: %v", err);
            }
        }
        gccgoMangler, err = pkgpath.ToSymbolFunc(cmd, objDir.val);
        if (err != null) {
            fatalf("%v", err);
        }
    }
    return gccgoMangler(ppath);
}

// Return the package prefix when using gccgo.
private static @string gccgoSymbolPrefix(this ptr<Package> _addr_p) {
    ref Package p = ref _addr_p.val;

    if (!gccgo.val) {
        return "";
    }
    if (gccgopkgpath != "".val) {
        return gccgoToSymbol(gccgopkgpath.val);
    }
    if (gccgoprefix == "" && p.PackageName == "main".val) {
        return "main";
    }
    var prefix = gccgoToSymbol(gccgoprefix.val);
    if (prefix == "") {
        prefix = "go";
    }
    return prefix + "." + p.PackageName;
}

// Call a function for each entry in an ast.FieldList, passing the
// index into the list, the name if any, and the type.
private static void forFieldList(ptr<ast.FieldList> _addr_fl, Action<nint, @string, ast.Expr> fn) {
    ref ast.FieldList fl = ref _addr_fl.val;

    if (fl == null) {
        return ;
    }
    nint i = 0;
    foreach (var (_, r) in fl.List) {
        if (r.Names == null) {
            fn(i, "", r.Type);
            i++;
        }
        else
 {
            foreach (var (_, n) in r.Names) {
                fn(i, n.Name, r.Type);
                i++;
            }
        }
    }
}

private static ptr<TypeRepr> c(@string repr, params object[] args) {
    args = args.Clone();

    return addr(new TypeRepr(repr,args));
}

// Map predeclared Go types to Type.
private static map goTypes = /* TODO: Fix this in ScannerBase_Expression::ExitCompositeLit */ new map<@string, ptr<Type>>{"bool":{Size:1,Align:1,C:c("GoUint8")},"byte":{Size:1,Align:1,C:c("GoUint8")},"int":{Size:0,Align:0,C:c("GoInt")},"uint":{Size:0,Align:0,C:c("GoUint")},"rune":{Size:4,Align:4,C:c("GoInt32")},"int8":{Size:1,Align:1,C:c("GoInt8")},"uint8":{Size:1,Align:1,C:c("GoUint8")},"int16":{Size:2,Align:2,C:c("GoInt16")},"uint16":{Size:2,Align:2,C:c("GoUint16")},"int32":{Size:4,Align:4,C:c("GoInt32")},"uint32":{Size:4,Align:4,C:c("GoUint32")},"int64":{Size:8,Align:8,C:c("GoInt64")},"uint64":{Size:8,Align:8,C:c("GoUint64")},"float32":{Size:4,Align:4,C:c("GoFloat32")},"float64":{Size:8,Align:8,C:c("GoFloat64")},"complex64":{Size:8,Align:4,C:c("GoComplex64")},"complex128":{Size:16,Align:8,C:c("GoComplex128")},};

// Map an ast type to a Type.
private static ptr<Type> cgoType(this ptr<Package> _addr_p, ast.Expr e) {
    ref Package p = ref _addr_p.val;

    switch (e.type()) {
        case ptr<ast.StarExpr> t:
            var x = p.cgoType(t.X);
            return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("%s*",x.C)));
            break;
        case ptr<ast.ArrayType> t:
            if (t.Len == null) { 
                // Slice: pointer, len, cap.
                return addr(new Type(Size:p.PtrSize*3,Align:p.PtrSize,C:c("GoSlice")));
            } 
            // Non-slice array types are not supported.
            break;
        case ptr<ast.StructType> t:
            break;
        case ptr<ast.FuncType> t:
            return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("void*")));
            break;
        case ptr<ast.InterfaceType> t:
            return addr(new Type(Size:2*p.PtrSize,Align:p.PtrSize,C:c("GoInterface")));
            break;
        case ptr<ast.MapType> t:
            return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("GoMap")));
            break;
        case ptr<ast.ChanType> t:
            return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("GoChan")));
            break;
        case ptr<ast.Ident> t:
            foreach (var (_, d) in p.Decl) {
                ptr<ast.GenDecl> (gd, ok) = d._<ptr<ast.GenDecl>>();
                if (!ok || gd.Tok != token.TYPE) {
                    continue;
                }
                foreach (var (_, spec) in gd.Specs) {
                    ptr<ast.TypeSpec> (ts, ok) = spec._<ptr<ast.TypeSpec>>();
                    if (!ok) {
                        continue;
                    }
                    if (ts.Name.Name == t.Name) {
                        return _addr_p.cgoType(ts.Type)!;
                    }
                }
            }
            {
                var def = typedef[t.Name];

                if (def != null) {
                    return _addr_def!;
                }

            }
            if (t.Name == "uintptr") {
                return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("GoUintptr")));
            }
            if (t.Name == "string") { 
                // The string data is 1 pointer + 1 (pointer-sized) int.
                return addr(new Type(Size:2*p.PtrSize,Align:p.PtrSize,C:c("GoString")));
            }
            if (t.Name == "error") {
                return addr(new Type(Size:2*p.PtrSize,Align:p.PtrSize,C:c("GoInterface")));
            }
            {
                var (r, ok) = goTypes[t.Name];

                if (ok) {
                    if (r.Size == 0) { // int or uint
                        ptr<Type> rr = @new<Type>();
                        rr.val = r.val;
                        rr.Size = p.IntSize;
                        rr.Align = p.IntSize;
                        r = rr;
                    }
                    if (r.Align > p.PtrSize) {
                        r.Align = p.PtrSize;
                    }
                    return _addr_r!;
                }

            }
            error_(e.Pos(), "unrecognized Go type %s", t.Name);
            return addr(new Type(Size:4,Align:4,C:c("int")));
            break;
        case ptr<ast.SelectorExpr> t:
            ptr<ast.Ident> (id, ok) = t.X._<ptr<ast.Ident>>();
            if (ok && id.Name == "unsafe" && t.Sel.Name == "Pointer") {
                return addr(new Type(Size:p.PtrSize,Align:p.PtrSize,C:c("void*")));
            }
            break;
    }
    error_(e.Pos(), "Go type not supported in export: %s", gofmt(e));
    return addr(new Type(Size:4,Align:4,C:c("int")));
}

private static readonly @string gccProlog = @"
#line 1 ""cgo-gcc-prolog""
/*
  If x and y are not equal, the type will be invalid
  (have a negative array count) and an inscrutable error will come
  out of the compiler and hopefully mention ""name"".
*/
#define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];

/* Check at compile time that the sizes we use match our expectations. */
#define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), n, _cgo_sizeof_##t##_is_not_##n)

__cgo_size_assert(char, 1)
__cgo_size_assert(short, 2)
__cgo_size_assert(int, 4)
typedef long long __cgo_long_long;
__cgo_size_assert(__cgo_long_long, 8)
__cgo_size_assert(float, 4)
__cgo_size_assert(double, 8)

extern char* _cgo_topofstack(void);

/*
  We use packed structs, but they are always aligned.
  The pragmas and address-of-packed-member are only recognized as warning
  groups in clang 4.0+, so ignore unknown pragmas first.
*/
#pragma GCC diagnostic ignored ""-Wunknown-pragmas""
#pragma GCC diagnostic ignored ""-Wpragmas""
#pragma GCC diagnostic ignored ""-Waddress-of-packed-member""

#include <errno.h>
#include <string.h>
";

// Prologue defining TSAN functions in C.


// Prologue defining TSAN functions in C.
private static readonly @string noTsanProlog = "\n#define CGO_NO_SANITIZE_THREAD\n#define _cgo_tsan_acquire()\n#define _cgo_tsan_rel" +
    "ease()\n";

// This must match the TSAN code in runtime/cgo/libcgo.h.
// This is used when the code is built with the C/C++ Thread SANitizer,
// which is not the same as the Go race detector.
// __tsan_acquire tells TSAN that we are acquiring a lock on a variable,
// in this case _cgo_sync. __tsan_release releases the lock.
// (There is no actual lock, we are just telling TSAN that there is.)
//
// When we call from Go to C we call _cgo_tsan_acquire.
// When the C function returns we call _cgo_tsan_release.
// Similarly, when C calls back into Go we call _cgo_tsan_release
// and then call _cgo_tsan_acquire when we return to C.
// These calls tell TSAN that there is a serialization point at the C call.
//
// This is necessary because TSAN, which is a C/C++ tool, can not see
// the synchronization in the Go code. Without these calls, when
// multiple goroutines call into C code, TSAN does not understand
// that the calls are properly synchronized on the Go side.
//
// To be clear, if the calls are not properly synchronized on the Go side,
// we will be hiding races. But when using TSAN on mixed Go C/C++ code
// it is more important to avoid false positives, which reduce confidence
// in the tool, than to avoid false negatives.


// This must match the TSAN code in runtime/cgo/libcgo.h.
// This is used when the code is built with the C/C++ Thread SANitizer,
// which is not the same as the Go race detector.
// __tsan_acquire tells TSAN that we are acquiring a lock on a variable,
// in this case _cgo_sync. __tsan_release releases the lock.
// (There is no actual lock, we are just telling TSAN that there is.)
//
// When we call from Go to C we call _cgo_tsan_acquire.
// When the C function returns we call _cgo_tsan_release.
// Similarly, when C calls back into Go we call _cgo_tsan_release
// and then call _cgo_tsan_acquire when we return to C.
// These calls tell TSAN that there is a serialization point at the C call.
//
// This is necessary because TSAN, which is a C/C++ tool, can not see
// the synchronization in the Go code. Without these calls, when
// multiple goroutines call into C code, TSAN does not understand
// that the calls are properly synchronized on the Go side.
//
// To be clear, if the calls are not properly synchronized on the Go side,
// we will be hiding races. But when using TSAN on mixed Go C/C++ code
// it is more important to avoid false positives, which reduce confidence
// in the tool, than to avoid false negatives.
private static readonly @string yesTsanProlog = @"
#line 1 ""cgo-tsan-prolog""
#define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))

long long _cgo_sync __attribute__ ((common));

extern void __tsan_acquire(void*);
extern void __tsan_release(void*);

__attribute__ ((unused))
static void _cgo_tsan_acquire() {
	__tsan_acquire(&_cgo_sync);
}

__attribute__ ((unused))
static void _cgo_tsan_release() {
	__tsan_release(&_cgo_sync);
}
";

// Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc.


// Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc.
private static var tsanProlog = noTsanProlog;

// noMsanProlog is a prologue defining an MSAN function in C.
// This is used when not compiling with -fsanitize=memory.
private static readonly @string noMsanProlog = "\n#define _cgo_msan_write(addr, sz)\n";

// yesMsanProlog is a prologue defining an MSAN function in C.
// This is used when compiling with -fsanitize=memory.
// See the comment above where _cgo_msan_write is called.


// yesMsanProlog is a prologue defining an MSAN function in C.
// This is used when compiling with -fsanitize=memory.
// See the comment above where _cgo_msan_write is called.
private static readonly @string yesMsanProlog = "\nextern void __msan_unpoison(const volatile void *, size_t);\n\n#define _cgo_msan_w" +
    "rite(addr, sz) __msan_unpoison((addr), (sz))\n";

// msanProlog is set to yesMsanProlog if we see -fsanitize=memory in the flags
// for the C compiler.


// msanProlog is set to yesMsanProlog if we see -fsanitize=memory in the flags
// for the C compiler.
private static var msanProlog = noMsanProlog;

private static readonly @string builtinProlog = @"
#line 1 ""cgo-builtin-prolog""
#include <stddef.h> /* for ptrdiff_t and size_t below */

/* Define intgo when compiling with GCC.  */
typedef ptrdiff_t intgo;

#define GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; intgo n; } _GoString_;
typedef struct { char *p; intgo n; intgo c; } _GoBytes_;
_GoString_ GoString(char *p);
_GoString_ GoStringN(char *p, int l);
_GoBytes_ GoBytes(void *p, int n);
char *CString(_GoString_);
void *CBytes(_GoBytes_);
void *_CMalloc(size_t);

__attribute__ ((unused))
static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; }

__attribute__ ((unused))
static const char *_GoStringPtr(_GoString_ s) { return s.p; }
";



private static readonly @string goProlog = @"
//go:linkname _cgo_runtime_cgocall runtime.cgocall
func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32

//go:linkname _cgoCheckPointer runtime.cgoCheckPointer
func _cgoCheckPointer(interface{}, interface{})

//go:linkname _cgoCheckResult runtime.cgoCheckResult
func _cgoCheckResult(interface{})
";



private static readonly @string gccgoGoProlog = "\nfunc _cgoCheckPointer(interface{}, interface{})\n\nfunc _cgoCheckResult(interface{" +
    "})\n";



private static readonly @string goStringDef = "\n//go:linkname _cgo_runtime_gostring runtime.gostring\nfunc _cgo_runtime_gostring(" +
    "*_Ctype_char) string\n\nfunc _Cfunc_GoString(p *_Ctype_char) string {\n\treturn _cgo" +
    "_runtime_gostring(p)\n}\n";



private static readonly @string goStringNDef = "\n//go:linkname _cgo_runtime_gostringn runtime.gostringn\nfunc _cgo_runtime_gostrin" +
    "gn(*_Ctype_char, int) string\n\nfunc _Cfunc_GoStringN(p *_Ctype_char, l _Ctype_int" +
    ") string {\n\treturn _cgo_runtime_gostringn(p, int(l))\n}\n";



private static readonly @string goBytesDef = "\n//go:linkname _cgo_runtime_gobytes runtime.gobytes\nfunc _cgo_runtime_gobytes(uns" +
    "afe.Pointer, int) []byte\n\nfunc _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []" +
    "byte {\n\treturn _cgo_runtime_gobytes(p, int(l))\n}\n";



private static readonly @string cStringDef = "\nfunc _Cfunc_CString(s string) *_Ctype_char {\n\tp := _cgo_cmalloc(uint64(len(s)+1)" +
    ")\n\tpp := (*[1<<30]byte)(p)\n\tcopy(pp[:], s)\n\tpp[len(s)] = 0\n\treturn (*_Ctype_char" +
    ")(p)\n}\n";



private static readonly @string cBytesDef = "\nfunc _Cfunc_CBytes(b []byte) unsafe.Pointer {\n\tp := _cgo_cmalloc(uint64(len(b)))" +
    "\n\tpp := (*[1<<30]byte)(p)\n\tcopy(pp[:], b)\n\treturn p\n}\n";



private static readonly @string cMallocDef = "\nfunc _Cfunc__CMalloc(n _Ctype_size_t) unsafe.Pointer {\n\treturn _cgo_cmalloc(uint" +
    "64(n))\n}\n";



private static map builtinDefs = /* TODO: Fix this in ScannerBase_Expression::ExitCompositeLit */ new map<@string, @string>{"GoString":goStringDef,"GoStringN":goStringNDef,"GoBytes":goBytesDef,"CString":cStringDef,"CBytes":cBytesDef,"_CMalloc":cMallocDef,};

// Definitions for C.malloc in Go and in C. We define it ourselves
// since we call it from functions we define, such as C.CString.
// Also, we have historically ensured that C.malloc does not return
// nil even for an allocation of 0.

private static readonly @string cMallocDefGo = @"
//go:cgo_import_static _cgoPREFIX_Cfunc__Cmalloc
//go:linkname __cgofn__cgoPREFIX_Cfunc__Cmalloc _cgoPREFIX_Cfunc__Cmalloc
var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)

//go:linkname runtime_throw runtime.throw
func runtime_throw(string)

//go:cgo_unsafe_args
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
	_cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
	if r1 == nil {
		runtime_throw(""runtime: C malloc failed"")
	}
	return
}
";

// cMallocDefC defines the C version of C.malloc for the gc compiler.
// It is defined here because C.CString and friends need a definition.
// We define it by hand, rather than simply inventing a reference to
// C.malloc, because <stdlib.h> may not have been included.
// This is approximately what writeOutputFunc would generate, but
// skips the cgo_topofstack code (which is only needed if the C code
// calls back into Go). This also avoids returning nil for an
// allocation of 0 bytes.


// cMallocDefC defines the C version of C.malloc for the gc compiler.
// It is defined here because C.CString and friends need a definition.
// We define it by hand, rather than simply inventing a reference to
// C.malloc, because <stdlib.h> may not have been included.
// This is approximately what writeOutputFunc would generate, but
// skips the cgo_topofstack code (which is only needed if the C code
// calls back into Go). This also avoids returning nil for an
// allocation of 0 bytes.
private static readonly @string cMallocDefC = @"
CGO_NO_SANITIZE_THREAD
void _cgoPREFIX_Cfunc__Cmalloc(void *v) {
	struct {
		unsigned long long p0;
		void *r1;
	} PACKED *a = v;
	void *ret;
	_cgo_tsan_acquire();
	ret = malloc(a->p0);
	if (ret == 0 && a->p0 == 0) {
		ret = malloc(1);
	}
	a->r1 = ret;
	_cgo_tsan_release();
}
";



private static @string cPrologGccgo(this ptr<Package> _addr_p) {
    ref Package p = ref _addr_p.val;

    var r = strings.NewReplacer("PREFIX", cPrefix, "GCCGOSYMBOLPREF", p.gccgoSymbolPrefix(), "_cgoCheckPointer", gccgoToSymbol("_cgoCheckPointer"), "_cgoCheckResult", gccgoToSymbol("_cgoCheckResult"));
    return r.Replace(cPrologGccgo);
}

private static readonly @string cPrologGccgo = "\n#line 1 \"cgo-c-prolog-gccgo\"\n#include <stdint.h>\n#include <stdlib.h>\n#include <s" +
    "tring.h>\n\ntypedef unsigned char byte;\ntypedef intptr_t intgo;\n\nstruct __go_strin" +
    "g {\n\tconst unsigned char *__data;\n\tintgo __length;\n};\n\ntypedef struct __go_open_" +
    "array {\n\tvoid* __values;\n\tintgo __count;\n\tintgo __capacity;\n} Slice;\n\nstruct __g" +
    "o_string __go_byte_array_to_string(const void* p, intgo len);\nstruct __go_open_a" +
    "rray __go_string_to_byte_array (struct __go_string str);\n\nextern void runtime_th" +
    "row(const char *);\n\nconst char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {" +
    "\n\tchar *p = malloc(s.__length+1);\n\tif(p == NULL)\n\t\truntime_throw(\"runtime: C mal" +
    "loc failed\");\n\tmemmove(p, s.__data, s.__length);\n\tp[s.__length] = 0;\n\treturn p;\n" +
    "}\n\nvoid *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) {\n\tchar *p = malloc(b" +
    ".__count);\n\tif(p == NULL)\n\t\truntime_throw(\"runtime: C malloc failed\");\n\tmemmove(" +
    "p, b.__values, b.__count);\n\treturn p;\n}\n\nstruct __go_string _cgoPREFIX_Cfunc_GoS" +
    "tring(char *p) {\n\tintgo len = (p != NULL) ? strlen(p) : 0;\n\treturn __go_byte_arr" +
    "ay_to_string(p, len);\n}\n\nstruct __go_string _cgoPREFIX_Cfunc_GoStringN(char *p, " +
    "int32_t n) {\n\treturn __go_byte_array_to_string(p, n);\n}\n\nSlice _cgoPREFIX_Cfunc_" +
    "GoBytes(char *p, int32_t n) {\n\tstruct __go_string s = { (const unsigned char *)p" +
    ", n };\n\treturn __go_string_to_byte_array(s);\n}\n\nvoid *_cgoPREFIX_Cfunc__CMalloc(" +
    "size_t n) {\n\tvoid *p = malloc(n);\n\tif(p == NULL && n == 0)\n\t\tp = malloc(1);\n\tif(" +
    "p == NULL)\n\t\truntime_throw(\"runtime: C malloc failed\");\n\treturn p;\n}\n\nstruct __g" +
    "o_type_descriptor;\ntypedef struct __go_empty_interface {\n\tconst struct __go_type" +
    "_descriptor *__type_descriptor;\n\tvoid *__object;\n} Eface;\n\nextern void runtimeCg" +
    "oCheckPointer(Eface, Eface)\n\t__asm__(\"runtime.cgoCheckPointer\")\n\t__attribute__((" +
    "weak));\n\nextern void localCgoCheckPointer(Eface, Eface)\n\t__asm__(\"GCCGOSYMBOLPRE" +
    "F._cgoCheckPointer\");\n\nvoid localCgoCheckPointer(Eface ptr, Eface arg) {\n\tif(run" +
    "timeCgoCheckPointer) {\n\t\truntimeCgoCheckPointer(ptr, arg);\n\t}\n}\n\nextern void run" +
    "timeCgoCheckResult(Eface)\n\t__asm__(\"runtime.cgoCheckResult\")\n\t__attribute__((wea" +
    "k));\n\nextern void localCgoCheckResult(Eface)\n\t__asm__(\"GCCGOSYMBOLPREF._cgoCheck" +
    "Result\");\n\nvoid localCgoCheckResult(Eface val) {\n\tif(runtimeCgoCheckResult) {\n\t\t" +
    "runtimeCgoCheckResult(val);\n\t}\n}\n";

// builtinExportProlog is a shorter version of builtinProlog,
// to be put into the _cgo_export.h file.
// For historical reasons we can't use builtinProlog in _cgo_export.h,
// because _cgo_export.h defines GoString as a struct while builtinProlog
// defines it as a function. We don't change this to avoid unnecessarily
// breaking existing code.
// The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
// error if a Go file with a cgo comment #include's the export header
// generated by a different package.


// builtinExportProlog is a shorter version of builtinProlog,
// to be put into the _cgo_export.h file.
// For historical reasons we can't use builtinProlog in _cgo_export.h,
// because _cgo_export.h defines GoString as a struct while builtinProlog
// defines it as a function. We don't change this to avoid unnecessarily
// breaking existing code.
// The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
// error if a Go file with a cgo comment #include's the export header
// generated by a different package.
private static readonly @string builtinExportProlog = @"
#line 1 ""cgo-builtin-export-prolog""

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif
";



private static @string gccExportHeaderProlog(this ptr<Package> _addr_p) {
    ref Package p = ref _addr_p.val;

    return strings.Replace(gccExportHeaderProlog, "GOINTBITS", fmt.Sprint(8 * p.IntSize), -1);
}

// gccExportHeaderProlog is written to the exported header, after the
// import "C" comment preamble but before the generated declarations
// of exported functions. This permits the generated declarations to
// use the type names that appear in goTypes, above.
//
// The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
// error if a Go file with a cgo comment #include's the export header
// generated by a different package. Unfortunately GoString means two
// different things: in this prolog it means a C name for the Go type,
// while in the prolog written into the start of the C code generated
// from a cgo-using Go file it means the C.GoString function. There is
// no way to resolve this conflict, but it also doesn't make much
// difference, as Go code never wants to refer to the latter meaning.
private static readonly @string gccExportHeaderProlog = @"
/* Start of boilerplate cgo prologue.  */
#line 1 ""cgo-gcc-export-header-prolog""

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoIntGOINTBITS GoInt;
typedef GoUintGOINTBITS GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern ""C"" {
#endif
";

// gccExportHeaderEpilog goes at the end of the generated header file.


// gccExportHeaderEpilog goes at the end of the generated header file.
private static readonly @string gccExportHeaderEpilog = "\n#ifdef __cplusplus\n}\n#endif\n";

// gccgoExportFileProlog is written to the _cgo_export.c file when
// using gccgo.
// We use weak declarations, and test the addresses, so that this code
// works with older versions of gccgo.


// gccgoExportFileProlog is written to the _cgo_export.c file when
// using gccgo.
// We use weak declarations, and test the addresses, so that this code
// works with older versions of gccgo.
private static readonly @string gccgoExportFileProlog = @"
#line 1 ""cgo-gccgo-export-file-prolog""
extern _Bool runtime_iscgo __attribute__ ((weak));

static void GoInit(void) __attribute__ ((constructor));
static void GoInit(void) {
	if(&runtime_iscgo)
		runtime_iscgo = 1;
}

extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(void) __attribute__ ((weak));
";


} // end main_package
